package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiDeleteAddress;
import com.lc.afbx.conn.GetApiMineAddress;
import com.lc.afbx.conn.PostApiUpdateAddresss;
import com.lc.afbx.dialog.AlertDialog;
import com.lc.afbx.util.MEditTextListener;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2016/6/1.
 */
public class AddressEditActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.address_add_isfirst_rl) private RelativeLayout address_add_isfirst_rl;
    @BoundView(R.id.address_add_isfirst_iv) private ImageView address_add_isfirst_iv;

    private EditText address_edit_name_et;
    private EditText address_edit_phone_et;
    private TextView address_edit_area_tv;
    private EditText address_edit_detail_address_et;

    private GetApiMineAddress.Data data;
    private boolean isFirst;

    private PostApiUpdateAddresss postApiUpdateAddresss = new PostApiUpdateAddresss(new AsyCallBack<PostApiUpdateAddresss.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiUpdateAddresss.Info info) throws Exception {

            MyToast.showToast(toast, AddressEditActivity.this);

            AddressActivity.RefreshAddressCallback refreshAddressCallback = (AddressActivity.RefreshAddressCallback) getAppCallBack(AddressActivity.class);
            refreshAddressCallback.refresh();

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    private GetApiDeleteAddress getApiDeleteAddress = new GetApiDeleteAddress(new AsyCallBack<GetApiDeleteAddress.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiDeleteAddress.Info info) throws Exception {

            UtilToast.show(AddressEditActivity.this, "删除成功");

            AddressActivity.RefreshAddressCallback refreshAddressCallback = (AddressActivity.RefreshAddressCallback) getAppCallBack(AddressActivity.class);
            refreshAddressCallback.refresh();

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }

    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_editaddress);

        address_edit_name_et = ((EditText) findViewById(R.id.address_edit_name_et));
        address_edit_phone_et = ((EditText) findViewById(R.id.address_edit_phone_et));
        address_edit_detail_address_et = ((EditText) findViewById(R.id.address_edit_detail_address_et));
        address_edit_area_tv = ((TextView) findViewById(R.id.address_edit_area_tv));

        address_edit_name_et.addTextChangedListener(new MEditTextListener(context, 16, address_edit_name_et));
        address_edit_phone_et.addTextChangedListener(new MEditTextListener(context, 11, address_edit_phone_et));
        address_edit_detail_address_et.addTextChangedListener(new MEditTextListener(context, 30, address_edit_detail_address_et));


        Bundle bundle = getIntent().getExtras();

        data = (GetApiMineAddress.Data) bundle.getSerializable("data");

        //上页带过来值
        address_edit_name_et.setText(data.rec_name);
        address_edit_phone_et.setText(data.phone);
        address_edit_area_tv.setText(data.cur_address);
        address_edit_detail_address_et.setText(data.road_address);

        if (Boolean.parseBoolean(data.is_first)) {

            isFirst = true;
            address_add_isfirst_iv.setImageResource(R.mipmap.szmrdz);

        } else {

            isFirst = false;
            address_add_isfirst_iv.setImageResource(R.mipmap.lanquan);

        }

        address_edit_area_tv.setOnClickListener(this);
        address_add_isfirst_rl.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:

                finish();
                return;

            case R.id.address_edit_save_bt:

                String areaStr = address_edit_area_tv.getText().toString().trim().replaceAll(" ", "");
                String xiangxiAddressStr = address_edit_detail_address_et.getText().toString().trim().replaceAll(" ", "");
                String phoneNumStr = address_edit_phone_et.getText().toString().trim().replaceAll(" ", "");
                String nameStr = address_edit_name_et.getText().toString().trim().replaceAll(" ", "");

                if (TextUtils.isEmpty(nameStr)) {
                    MyToast.showToast("收件人姓名不能为空", context);
                    return;
                }
                if (TextUtils.isEmpty(phoneNumStr)) {
                    MyToast.showToast("手机号不能为空", context);
                    return;
                }
                if (!Validator.isMobile(phoneNumStr)) {
                    MyToast.showToast("手机号码不正确", context);
                    return;
                }
                if (TextUtils.isEmpty(areaStr)) {
                    MyToast.showToast("请选择所在地区", context);
                    return;
                }
                if (TextUtils.isEmpty(xiangxiAddressStr)) {
                    MyToast.showToast("详细地址不能为空", context);
                    return;
                }

                postApiUpdateAddresss.id = data.id;
                postApiUpdateAddresss.is_first = isFirst + "";
                postApiUpdateAddresss.cur_address = areaStr;
                postApiUpdateAddresss.road_address = xiangxiAddressStr;
                postApiUpdateAddresss.phone = phoneNumStr;
                postApiUpdateAddresss.rec_name = nameStr;
                postApiUpdateAddresss.userid = Integer.parseInt(BaseApplication.BasePreferences.readUserId());

                postApiUpdateAddresss.execute(this);

                return;

            case R.id.address_edit_delete_rl:

                new AlertDialog(this, "是否删除") {
                    @Override
                    protected void onConfirm() {
                        getApiDeleteAddress.id = String.valueOf(data.id);
                        getApiDeleteAddress.userid = BaseApplication.BasePreferences.readUserId();
                        getApiDeleteAddress.execute(context);
                    }

                    @Override
                    protected void onCancle() {

                    }
                }.show();

                return;

            case R.id.address_edit_area_tv:


                startActivityForResult(new Intent(this, ChoseAreaActivity.class).putExtra(PrefrenceConfig.INTENT_TAG, ""), 3);

                return;

            case R.id.address_add_isfirst_rl:

                if (!isFirst) {

                    isFirst = true;
                    address_add_isfirst_iv.setImageResource(R.mipmap.szmrdz);
                    MyToast.showToast("设为默认地址", this);

                } else {

                    isFirst = false;
                    address_add_isfirst_iv.setImageResource(R.mipmap.lanquan);
                    MyToast.showToast("不设为默认地址", this);

                }

                return;


        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

//        UtilLog.e("看看","这个方法走没走啊");
        switch (resultCode) {
            case 3:

                Bundle bundle = data.getExtras();

                ArrayList list = bundle.getParcelableArrayList("list");

                List<Map<String, String>> lists = (List<Map<String, String>>) list.get(0);

                StringBuffer sb = new StringBuffer();

                for (int i = 0; i < lists.size(); i++) {

                    sb.append(lists.get(i).get("title"));

                }

                address_edit_area_tv.setText(sb.toString());

                break;
            default:
                break;
        }
    }


}
